import os
from astropy.table import Table


def mask_bit_def():
    # mask bit definition
    # warm pixels (5 sigma) = 2
    # hot pixels (bad) = 32
    # dark pixel (< 50%) = 64
    # charge trap = 128
    # no data = 512
    """ py:func:`.dark.get_dmask`"""
    tab = Table()
    tab['name'] = ['nan', 'trap', 'dark', 'hot', 'warm']
    tab['value'] = [512, 128, 64, 32, 2]
    return tab


class ConfDummy:
    def __init__(self):
        self.name = 'dummy'
        self.type = 'dummy'
        self.overscan = False


class ConfE2V290:
    def __init__(self):
        self.name = 'e2v290'
        self.type = 'ccd'
#        self.tsys = 188
        self.tsys = 183
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9400, 10000)
        self.raw_x0 = [1250 * i for i in self.col]
        self.raw_y0 = [4700 * i for i in self.row]
        self.raw_nx = [1250] * 16
        self.raw_ny = [4700] * 16
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [27] * 16
        self.y1 = [0] * 16
        self.nx = [1152] * 16
        self.ny = [4616] * 16
        self.overscan_x1 = [1200] * 16
        self.overscan_x2 = [1250] * 16
        self.overscan_y1 = [4650] * 16
        self.overscan_y2 = [4700] * 16

        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16

class ConfE2V290_scan:
    def __init__(self,height):
        self.name = 'e2v290_scan'
        self.type = 'ccd'
        self.tsys = 198
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (height, 10000)
        self.raw_x0 = [1250 * i for i in self.col]
        self.raw_y0 = [int(height/2) * i for i in self.row]
        self.raw_nx = [1250] * 16
        self.raw_ny = [int(height/2)] * 16
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        self.raw_yflip = [0]*16

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [27] * 16
        self.y1 = [0] * 16
        self.nx = [1152] * 16
        self.ny = [int(height/2)] * 16
        self.overscan_x1 = [1200] * 16
        self.overscan_x2 = [1250] * 16
        self.overscan_y1 = [int(height/2)] * 16
        self.overscan_y2 = [int(height/2)] * 16

        # mosaic image (without prescan and overscan)
        self.mos_shape = (height, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [int(height/2) * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16

class Conf_xiguang:
    def __init__(self):
        self.name = 'xiguang'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9292, 9672)
        self.raw_x0 = [1209 * i for i in self.col]  # 通道数据图像起始x0
        self.raw_y0 = [4646 * i for i in self.row]  # 通道数据图像起始y0
        self.raw_nx = [1209] * 16                   # 各通道x向宽度
        self.raw_ny = [4646] * 16                   # 各通道y向宽度
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        #self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  #用于天体定标
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region
        self.x1 = [26] * 16              # 感光区x向开始序号
        self.y1 = [15] * 16              # 感光区y向开始序号
        self.nx = [1152] * 16            # 感光区x向宽度
        self.ny = [4616] * 16            # 感光区y向宽度
        self.overscan_x1 = [1188] * 16   # 通道内overscan x向起始位置
        self.overscan_x2 = [1208] * 16   # 通道内overscan x向终止位置
        self.overscan_y1 = [4636] * 16   # 通道内overscan y向起始位置
        self.overscan_y2 = [4646] * 16   # 通道内overscan y向终止位置
                
        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16

class Conf_xiguang_TTD:
    def __init__(self):
        self.name = 'xiguang'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9292, 9672)
        self.raw_x0 = [1209 * i for i in self.col]
        self.raw_y0 = [4646 * i for i in self.row]
        self.raw_nx = [1209] * 16
        self.raw_ny = [4646] * 16
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  #用于天体定标
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region
        self.x1 = [27] * 16
        self.y1 = [15] * 16
        self.nx = [1152] * 16
        self.ny = [4616] * 16
        self.overscan_x1 = [1189] * 16
        self.overscan_x2 = [1209] * 16
        self.overscan_y1 = [4636] * 16
        self.overscan_y2 = [4646] * 16
                
        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16



class Conf_44ccdmsc:
    # 适用于巡天模块鉴定件主焦面装机44所CCD；
    def __init__(self):
        self.name = '44ccdmsc'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9292, 9672)
        self.raw_x0 = [1209 * i for i in self.col]  # 通道数据起始x0
        self.raw_y0 = [4646 * i for i in self.row]  # 通道数据起始y0
        self.raw_nx = [1209] * 16                   # 各通道x向宽度
        self.raw_ny = [4646] * 16                   # 各通道y向高度
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        #self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  #用于天体定标
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region
        self.x1 = [28]+[26]*6+[28]*2+[26]*6+[28]        # 感光区x向开始序号
        self.y1 = [19] * 16              # 感光区y向开始序号
        self.nx = [1152] * 16            # 感光区x向宽度
        self.ny = [4616] * 16            # 感光区y向高度
        self.overscan_x1 = [1188] * 16   # 通道内overscan x向起始位置
        self.overscan_x2 = [1207] * 16   # 通道内overscan x向终止位置
        self.overscan_y1 = [4637] * 16   # 通道内overscan y向起始位置
        self.overscan_y2 = [4645] * 16   # 通道内overscan y向终止位置
                
        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16


class ConfGM1201A:
    def __init__(self):
        self.name = 'gm1201a'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9400, 10000)
        self.raw_x0 = [1250 * i for i in self.col]
        self.raw_y0 = [4700 * i for i in self.row]
        self.raw_nx = [1250] * 16
        self.raw_ny = [4700] * 16
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [29] * 16
        self.y1 = [4] * 16
        self.nx = [1152] * 16
        self.ny = [4616] * 16
        self.overscan_x1 = [1200] * 16
        self.overscan_x2 = [1250] * 16
        self.overscan_y1 = [4650] * 16
        self.overscan_y2 = [4700] * 16

        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16

class ConfGM1201A_self:
    def __init__(self):
        self.name = 'gm1201a_self'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 4, 0, 4, 1, 5, 1, 5, 6, 2, 2, 6, 7, 3, 3, 7]
        self.row = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (12000, 10000)
        self.raw_x0 = [1500 * i for i in self.col]
        self.raw_y0 = [5000 * i for i in self.row]
        self.raw_nx = [1500] * 16
        self.raw_ny = [5000] * 16
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [29] * 16
        self.y1 = [379] * 16
        self.nx = [1152] * 16
        self.ny = [4616] * 16
        self.overscan_x1 = [1181] * 16
        self.overscan_x2 = [1500] * 16
        self.overscan_y1 = [0] * 16
        self.overscan_y2 = [380] * 16

        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16

class ConfGM1201A_lower:
    def __init__(self):
        self.name = 'gm1201a_lower'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 8
        self.col = [0, 1, 2, 3, 4, 5, 6, 7]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0]
        self.overscan = 1

        # raw image
        self.raw_shape = (9400, 10000)
        self.raw_x0 = [1250 * i for i in self.col]
        self.raw_y0 = [4700 * i for i in self.row]
        self.raw_nx = [1250] * 8
        self.raw_ny = [4700] * 8
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1]  # flip for overscan region alignment
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [29] * 8
        self.y1 = [4] * 8
        self.nx = [1152] * 8
        self.ny = [4616] * 8
        self.overscan_x1 = [1200] * 8
        self.overscan_x2 = [1250] * 8
        self.overscan_y1 = [4650] * 8
        self.overscan_y2 = [4700] * 8

        # mosaic image (without prescan and overscan)
        self.mos_shape = (4616, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 8  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 8

class ConfGM1201A_upper:
    def __init__(self):
        self.name = 'gm1201a_upper'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 8
        self.col = [7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9400, 10000)
        self.raw_x0 = [1250 * i for i in self.col]
        self.raw_y0 = [4700 * i for i in self.row]
        self.raw_nx = [1250] * 8
        self.raw_ny = [4700] * 8
        self.raw_xflip = [1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        self.raw_yflip = [1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [29] * 8
        self.y1 = [4] * 8
        self.nx = [1152] * 8
        self.ny = [4616] * 8
        self.overscan_x1 = [1200] * 8
        self.overscan_x2 = [1250] * 8
        self.overscan_y1 = [4650] * 8
        self.overscan_y2 = [4700] * 8

        # mosaic image (without prescan and overscan)
        self.mos_shape = (4616, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 8  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 8

class ConfHR9090BSI:
    def __init__(self):
        self.name = 'hr9090bsi'
        self.type = 'cmos'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 2
        self.col = [0, 1]
        self.row = [0, 0]
        self.overscan = 1

        # raw image
        self.raw_shape = (9222, 8976)
        self.raw_x0 = [0, 4352]
        self.raw_y0 = [0, 0]
        self.raw_nx = [4352, 4624]
        self.raw_ny = [9222, 9222]
        self.raw_xflip = [0, 1]  # flip for overscan region alignment
        self.raw_yflip = [0, 0]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [38, 38]  # EB 34 + dummy 4
        self.y1 = [92, 92]  # dummy 92
        self.nx = [4314, 4586]
        self.ny = [9120, 9120]
        self.overscan_x1 = [0, 0]
        self.overscan_x2 = [34, 34]  # only use EB
        self.overscan_y1 = [0, 0]
        self.overscan_y2 = [92, 92]

        # mosaic image (without prescan and overscan)
        self.mos_shape = (9120, 8900)
        self.mos_x0 = [0, 4314]
        self.mos_y0 = [0, 0]
        self.mos_xflip = [0, 0]  # flip the exposure region for mosaic
        self.mos_yflip = [0, 0]
        
class ConfHR9090BSI_scan:
    def __init__(self,height):
        self.name = 'hr9090bsi_scan'
        self.type = 'cmos'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 2
        self.col = [0, 1]
        self.row = [0, 0]
        self.overscan = 1

        # raw image
        self.raw_shape = (height, 8976)
        self.raw_x0 = [0, 4352]
        self.raw_y0 = [0, 0]
        self.raw_nx = [4352, 4624]
        self.raw_ny = [height, height]
        self.raw_xflip = [0, 0]  # flip for overscan region alignment
        self.raw_yflip = [0, 0]

        # exposure region, prescan and overscan (in raw-flipped channel cutout)
        self.x1 = [38, 38]  # EB 34 + dummy 4
        self.y1 = [0, 0]  # dummy 92
        self.nx = [4314, 4586]
        self.ny = [height, height]
        self.overscan_x1 = [0, 0]
        self.overscan_x2 = [34, 34]  # only use EB
        self.overscan_y1 = [0, 0]
        self.overscan_y2 = [0, 0]
        
        # mosaic image (without prescan and overscan)
        self.mos_shape = (height, 8900)
        self.mos_x0 = [0, 4314]
        self.mos_y0 = [0, 0]
        self.mos_xflip = [0, 0]  # flip the exposure region for mosaic
        self.mos_yflip = [0, 0]


class Conf_yunda:
    def __init__(self):
        self.name = 'yunda'
        self.type = 'ccd'
        self.tsys = 188
        self.glass = 'none'
        self.wave = -99.0

        # channel layout
        self.nchan = 16
        self.col = [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
        self.row = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        self.overscan = 1

        # raw image
        self.raw_shape = (9400, 10000)
        self.raw_x0 = [1250 * i for i in self.col]  # 通道数据图像起始x0
        self.raw_y0 = [4700 * i for i in self.row]  # 通道数据图像起始y0
        self.raw_nx = [1250] * 16                   # 各通道x向宽度
        self.raw_ny = [4700] * 16                   # 各通道y向宽度
        self.raw_xflip = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]  # flip for overscan region alignment
        #self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  #用于天体定标
        self.raw_yflip = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]

        # exposure region
        self.x1 = [27] * 16              # 感光区x向开始序号
        self.y1 = [0] * 16              # 感光区y向开始序号
        self.nx = [1152] * 16            # 感光区x向宽度
        self.ny = [4616] * 16            # 感光区y向宽度
        self.overscan_x1 = [1179] * 16   # 通道内overscan x向起始位置
        self.overscan_x2 = [1249] * 16   # 通道内overscan x向终止位置
        self.overscan_y1 = [4617] * 16   # 通道内overscan y向起始位置
        self.overscan_y2 = [4699] * 16   # 通道内overscan y向终止位置
                
        # mosaic image (without prescan and overscan)
        self.mos_shape = (9232, 9216)
        self.mos_x0 = [1152 * i for i in self.col]
        self.mos_y0 = [4616 * i for i in self.row]
        self.mos_xflip = [0] * 16  # flip the exposure region for mosaic
        self.mos_yflip = [0] * 16

def load_imgconfig(name,height=None):
    valid = ['e2v290', 'e2v290_scan', 'cmos', 'hr9090bsi', '44ccd', 'gm1201a','cmos_scan','hr9090bsi_scan','ConfGM1201A_lower','ConfGM1201A_upper','gm1201a_self','xiguang','xiguang_TTD','yunda']
    if name is None:
        if 'LABTEST_CAMTYPE' in os.environ:
            name = os.environ['LABTEST_CAMTYPE']
        else:
            print('Valid configurations: {}'.format(', '.join(valid)))
            raise Exception('No valid configuration. Check environment variable LABTEST_CAMTYPE.')

    if name.lower() == 'e2v290':
        return ConfE2V290()
    if name.lower() == 'e2v290_scan':
        return ConfE2V290_scan(height)
    if name.lower() == 'cmos' or name.lower() == 'hr9090bsi':
        return ConfHR9090BSI()
    if name.lower() == 'cmos_scan' or name.lower() == 'hr9090bsi_scan':
        return ConfHR9090BSI_scan(height)
    if name.lower() == '44ccd' or name.lower() == 'gm1201a':
        return ConfGM1201A()
    if name.lower() == '44ccdmsc':
        return Conf_44ccdmsc()
    if name.lower() == 'gm1201a_self':
        return ConfGM1201A_self()
    if name.lower() == '44ccd_lower':
        return ConfGM1201A_lower()
    if name.lower() == '44ccd_upper':
        return ConfGM1201A_upper()
    if name.lower() == 'xiguang':
        return Conf_xiguang()
    if name.lower() == 'TTD_flag':
        return Conf_xiguang_TTD()
    if name.lower() == 'yunda':
        return Conf_yunda()
    print('Valid configurations: {}'.format(', '.join(valid)))
    raise Exception('Invalid configuration: {}'.format(name))


def load_calib(name):
    if name is None:
        print('no monitor name given')
        return None
    if os.path.exists(name):
        print('input monitor database table: {}'.format(name))
        return name

    # default database
    if name == '6k024':
        fcal = 'db_6K024.tab'
    elif name == '1b004':
        fcal = 'db_1B004.tab'
    elif name == 's2281':
        fcal = 'db_S2281.tab'
    else:
        fcal = None

    if fcal is None:
        print('not proper database table: {}'.format(name))
    else:
        if 'LABTEST_DATABASE' in os.environ:
            path = os.environ['LABTEST_DATABASE']
        else:
            path = '/data/data/calibration/monitor'
        fcal = '{}/{}'.format(path, fcal)
        if os.path.exists(fcal):
            print('use database table: {}'.format(fcal))
        else:
            print('no valid database found: {}'.format(fcal))
            fcal = None
    return fcal


def load_glass(name):
    if name is None:
        print('no glass database name given')
        return None
    if os.path.exists(name):
        print('input glass database table: {}'.format(name))
        return name

    # default database
    if name == 'none':
        ftrans = None
    elif name == '44':
        ftrans = 'db_glass_44.tab'
    elif name == 'mpf':
        ftrans = 'db_glass_MPF_A0651-7-CF.tab'
    elif name == 'hpfs7980':
        ftrans = 'db_glass_hpfs7980.tab'
    elif name == '3mm':
        ftrans = 'db_glass_sapphire3mm.tab'
    elif name == '5mm':
        ftrans = 'db_glass_sapphire5mm.tab'
    else:
        ftrans = None

    if ftrans is None:
        print('not proper database table: {}'.format(name))
    else:
        if 'LABTEST_DATABASE' in os.environ:
            path = os.environ['LABTEST_DATABASE']
        else:
            path = '/data/data/calibration/glass'
        ftrans = '{}/{}'.format(path, ftrans)
        if os.path.exists(ftrans):
            print('use database table: {}'.format(ftrans))
        else:
            print('no valid database found: {}'.format(ftrans))
            ftrans = None
    return ftrans
